package cn.gov.pbc.tims.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.pbc.tims.config.AppConfig;
import cn.gov.pbc.tims.entity.Type;
import cn.gov.pbc.tims.entity.Typegroup;
import cn.gov.pbc.tims.service.DictService;
import cn.gov.pbc.tims.util.CheckItem;

/**
 * 类型字段处理类
 * 
 * 
 */
@Controller
@RequestMapping("/dict")
public class DictController   {
	private static final Logger LOGGER = LogManager.getLogger(DictController.class);
	private DictService systemService;
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Autowired
	public void setSystemService(DictService systemService) {
		this.systemService = systemService;
	}

 
	/**
	 * 类型字典列表页面
	 * 
	 * @return
	 */
	@RequestMapping("typeGridTree")
	public @ResponseBody List typeGroupTabs(HttpServletRequest request) {
		List<Typegroup> typegroupList = systemService.loadAll(Typegroup.class);
		System.out.println(typegroupList);
		return typegroupList;
	}

	/**
	 *  
	 * 
	 * @return
	 */
	@RequestMapping("getTypesByGroupCode")
	public @ResponseBody List getTypesByGroup(HttpServletRequest request,String typegroupcode) {
		List<Type> typegroupList = systemService.getTypesByGroupCode(typegroupcode);
		//System.out.println(typegroupList);
		return typegroupList;
	}
	/**
	 *  
	 * 
	 * @return
	 */
	@RequestMapping("getTypesByGroupId")
	public @ResponseBody List getTypesByGroup(Integer typegroupId) {
		List<Type> typegroupList = systemService.getTypesByGroupId(typegroupId);
		//System.out.println(typegroupList);
		return typegroupList;
	}
	 
	/**
	 *  
	 * 
	 * @return
	 */
	@RequestMapping("getModel")
	public @ResponseBody JsonNode getModel(String modelName) throws JsonProcessingException, IOException {
		// System.out.println(modelName); 
		 ObjectMapper objectMapper=new ObjectMapper();
		 JsonNode jsonNode=objectMapper.readTree(new File(this.getClass().getResource("/json/model.json").getPath()));	  	  
		//JsonNode jsonNode=AppConfig.getJson("model");
		//List<City> citys = systemService.findByExample("City",city);
		 JsonNode model=jsonNode.get(modelName);
		return  model;
	} 
	@RequestMapping("/getYears")
	public @ResponseBody List<CheckItem> getCheckYearList(){
		List<CheckItem> list = new ArrayList<CheckItem>();
		CheckItem checkYear;
		Calendar cld = Calendar.getInstance();
		int currYear = cld.get(Calendar.YEAR),n = 0;
		for (int i = currYear - 5; i<currYear + 6; i++){
			checkYear = new CheckItem();
			checkYear.setValue(String.valueOf(i));
			checkYear.setLabel(String.valueOf(i));
			/*if (i==currYear){
				checkYear.setSelected(true);
			}*/
			list.add(n, checkYear);
			n++;
		}
		return list;
	}
	 
	 
 
 
	 
	
	
}
